
import java.util.Random;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * 
 * @author hanying zheng
 *
 */
public class WelcomeScreenLv2 extends GameCanvas implements Runnable, CommandListener {

	private Thread thread;
	
	private MobilePong parent = null;
	
	private final static Random random = new Random();
	
	private final int height;
	
	private final int width;
	
	private String soundcheck;

        

        
        private String[] menus = {"SINGLE GAME", "HOST MULTIPLAYER GAME", "JOIN MULTIPLAYER GAME", "SPECIAL MODE", "SOUND : OFF", "HELP", "EXIT"};
	
	private final String[] optionMenus = {"MUSIC", "BACK"};
	
	private int selectedMenuItem = 0;
	
	private int selectedOptionMenuItem = 0;
	
	private final Font highLightedFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);

	private final Font normalFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

        private final Font smallFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
	
	private final Command selectCmd = new Command("select", Command.OK, 1);
	
	private final Command exitCmd = new Command("exit", Command.BACK, 2);
	
	private final static int T_SELECTING_MAIN_ITEM = 0;
	
	private final static int T_SELECTING_OPTION_ITEM = 1;
	
	private final static int T_SHOWING_HELP = 2;
	
	private final static int T_END_OF_SELECTING = -1;
	
	private int currOpType;
	
	private final BrickLv2[] titleBricks;
	
	private final static int[] titleCells = 
	{ 
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3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, 3, 3, 2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 3, 2, 3, 3, 2, 2, 2, 2, 2, 3, 3, 3, 
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 3, 3, 
		3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3
	};
	
	private int titleCellLen;
	
	public WelcomeScreenLv2(MobilePong parent) {
		super(false);
		this.parent = parent;
		
		height = getHeight();
		width = getWidth();

		addCommand(exitCmd);
		addCommand(selectCmd);
		setCommandListener(this);
		
		currOpType = T_SELECTING_MAIN_ITEM;
		
		titleCellLen = titleCells.length;
		int column;
		int row;
		titleBricks = new BrickLv2[titleCellLen];
		
		for (int i = titleCellLen - 1; i > -1; i--) {
			column = i % 70;
			row = i / 70;
			this.titleBricks[i] = new BrickLv2(20 + row * 3, 10 + column * 3, 4, 4, titleCells[i]);
		}
		
		thread = new Thread(this);
		thread.start();
	}
	
	public void paint(Graphics g) {
		g.setColor(0, 0, 0);
		g.fillRect(0, 0, width, height);
		int itemTop;
		int itemLeft;
		int rr = random.nextInt(100);
		BrickLv2 temp;
		if (rr > 85) {
			int color1 = random.nextInt(255);
                        int color2 = random.nextInt(255);
                        int color3 = random.nextInt(255);
			for (int i = titleCellLen - 1; i > -1; i--) {
				temp = this.titleBricks[i];
				if (temp.type == BrickLv2.T_FIXED) {
					temp.setColor(color1, color2, color3);
				}
			}
		}
		for (int i = titleCellLen - 1; i > -1; i--) {
			this.titleBricks[i].paint(g);
		}
		
		
                if (this.currOpType == T_SHOWING_HELP) {
			g.setColor(255, 255, 255); // white
			g.setFont(highLightedFont);
			g.drawString("How To Control", width / 20, height / 3, Graphics.TOP | Graphics.LEFT);
			g.setFont(smallFont);
			g.drawString("Move Left:LEFT KEY / NUM 4", width / 20,
					height / 3 + 30, Graphics.TOP | Graphics.LEFT);
			g.drawString("Move Right:RIGHT KEY / NUM 6", width / 20,
					height / 3 + 50, Graphics.TOP | Graphics.LEFT);
			g.drawString("Rotate Left:NUM 7 / *", width / 20, height / 3 + 70, Graphics.TOP | Graphics.LEFT);
			g.drawString("Rotate Right:NUM 9 / #", width / 20, height / 3 + 90, Graphics.TOP | Graphics.LEFT);
			g.setColor(255, 0, 0);
			g.fillRect(0, height / 3 + 130, width, highLightedFont.getHeight());
			g.setColor(255, 255, 255);
			g.setFont(highLightedFont);
			g.drawString("BACK", (width - 40) / 2, height / 3 + 130, Graphics.TOP | Graphics.LEFT);

		}
                else {
			if (audio.mode)
                            menus[4] = "SOUND : ON";
                        else if (!audio.mode)
                            menus[4] = "SOUND : OFF";
                        for (int i = 0, len = menus.length; i < len; i++) {
				itemTop = height / 3 + i * highLightedFont.getHeight();
				if (selectedMenuItem == i) {
					itemLeft = (width - menus[i].length() * 10) / 2;
					//shadow
					g.setColor(255, 0, 0);		//red
					g.fillRect(0, itemTop, width, highLightedFont.getHeight());
					//string
					g.setColor(255, 255, 255);	//white
					g.setFont(highLightedFont);
					g.drawString(menus[i], itemLeft, itemTop, Graphics.TOP | Graphics.LEFT);
				} else {
					itemLeft = (width - menus[i].length() * 7) / 2;
					//shadow
					g.setColor(0, 0, 0);		//black
					g.fillRect(0, itemTop, width, highLightedFont.getHeight());
					//string
					g.setColor(255, 255, 255);	//white
					g.setFont(normalFont);
					g.drawString(menus[i], itemLeft, itemTop, Graphics.TOP | Graphics.LEFT);
				}
			}
		}
	}

	public void run() {
		try {
			while (this.currOpType != T_END_OF_SELECTING) {
				repaint(0, 0, width, height);
				Thread.currentThread().sleep(40);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == this.selectCmd) {
			menuAction();
		} else if (arg0 == this.exitCmd) {
			parent.exitGame();
		}
	}
	
	public void keyPressed(int keyCode) {
		switch (this.currOpType) {
		case T_SELECTING_MAIN_ITEM:
			if (GameCanvas.DOWN == getGameAction(keyCode)) {
				if (this.selectedMenuItem < this.menus.length - 1) {
					this.selectedMenuItem++;
				}
			} else if (GameCanvas.UP == getGameAction(keyCode)) {
				if (this.selectedMenuItem > 0) {
					this.selectedMenuItem--;
				}
			} else if (GameCanvas.FIRE == getGameAction(keyCode)) {
				menuAction();
			}
			break;
//		case T_SELECTING_OPTION_ITEM:
//			if (GameCanvas.DOWN == getGameAction(keyCode)) {
//				if (this.selectedOptionMenuItem < 1) {
//					this.selectedOptionMenuItem++;
//				}
//			} else if (GameCanvas.UP == getGameAction(keyCode)) {
//				if (this.selectedOptionMenuItem > 0) {
//					this.selectedOptionMenuItem--;
//				}
//			} else if (GameCanvas.FIRE == getGameAction(keyCode)) {
//				optionMenuAction();
//			} else if (GameCanvas.LEFT == getGameAction(keyCode)) {
//				if (this.selectedOptionMenuItem == 0) {
//					audio.mode = false;
//				}
//			} else if (GameCanvas.RIGHT == getGameAction(keyCode)) {
//				if (this.selectedOptionMenuItem == 0) {
//					audio.mode = true;
//				}
//			}
//			break;
		case T_SHOWING_HELP:
			if ((GameCanvas.FIRE == getGameAction(keyCode)) || (GameCanvas.UP == getGameAction(keyCode))){
                            this.currOpType = T_SELECTING_MAIN_ITEM;
			}
			break;
		}
		
	}
	
	private void menuAction() {
		switch (this.selectedMenuItem) {
		case 0:			//single player game
			currOpType = T_END_OF_SELECTING;
			parent.startSingleGame();
			break;
		case 1:			//host multiplayer game
			currOpType = T_END_OF_SELECTING;
			parent.hostMultiplayerGame();
			break;
		case 2:			//join multiplayer game
			currOpType = T_END_OF_SELECTING;
			parent.joinMultiplayerGame();
			break;
		case 3:			//start game
			currOpType = T_END_OF_SELECTING;
			parent.startSpecialMode();
			break;
		case 4:{			//option
			if (!audio.mode){
                            audio.mode = true;
                        }
                        else if (audio.mode){
                            audio.mode = false;
                        }
                        currOpType = T_SELECTING_MAIN_ITEM;
			break;
                }
		case 5:			//help
			currOpType = T_SHOWING_HELP;
			break;
		case 6:			//exit
			currOpType = T_END_OF_SELECTING;
			parent.exitGame();
			break;
		}
	}
	

	private void optionMenuAction() {
		switch (this.selectedOptionMenuItem) {
		case 0:			//music switch
			break;
		case 1:			//back
			this.selectedOptionMenuItem = 0;
			this.currOpType = T_SELECTING_MAIN_ITEM;
			break;
		}
	}

        private void drawmenu(){

        }
}
